package com.linkhub.oms.data.sync.api.shopify.domain.response;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.linkhub.oms.data.sync.api.shopify.enums.FulfillmentOrderStatusEnum;
import lombok.*;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * @description FulfillmentOrderResponse
 */
@Data
@EqualsAndHashCode(callSuper = true)
@NoArgsConstructor
@AllArgsConstructor
public class FulfillmentOrderResponse extends BaseResponse implements Serializable {
    @Override
    @JSONField(serialize = false, deserialize = false)
    public boolean success() {
        return true;
    }

    @Override
    public int count() {
        return 0;
    }

    @Override
    public String maxUpdateTime() {
        return null;
    }

    public List<LineItem> getAllValidFulfillment() {
        List<LineItem> lineItemsList = new ArrayList<>();
        if (CollUtil.isNotEmpty(fulfillmentOrders)) {

            for (FulfillmentOrder fulfillmentOrder : fulfillmentOrders) {
                String status = fulfillmentOrder.getStatus();
                if (FulfillmentOrderStatusEnum.OPEN.getStatus().equals(status) || FulfillmentOrderStatusEnum.IN_PROGRESS.getStatus().equals(status)) {
                    List<LineItem> lineItems = fulfillmentOrder.getLineItems();
                    if (CollUtil.isNotEmpty(lineItems)) {
                        lineItemsList.addAll(lineItems);
                    }
                }
            }
        }
        return lineItemsList;
    }


    @JSONField(name = "fulfillment_orders")
    private List<FulfillmentOrder> fulfillmentOrders;

    @NoArgsConstructor
    @Data
    public static class FulfillmentOrder {
        private long id;
        @JsonProperty("shop_id")
        private long shopId;
        @JsonProperty("order_id")
        private long orderId;
        @JsonProperty("assigned_location_id")
        private long assignedLocationId;
        @JsonProperty("request_status")
        private String requestStatus;
        private String status;
        @JsonProperty("supported_actions")
        private List<String> supportedActions;
        private Destination destination;
        @JsonProperty("line_items")
        private List<LineItem> lineItems;
        @JsonProperty("international_duties")
        private String internationalDuties;
        @JsonProperty("fulfill_at")
        private String fulfillAt;
        @JsonProperty("fulfill_by")
        private String fulfillBy;
        @JsonProperty("fulfillment_holds")
        private List<String> fulfillmentHolds;
        @JsonProperty("created_at")
        private String createdAt;
        @JsonProperty("updated_at")
        private String updatedAt;
        @JsonProperty("delivery_method")
        private DeliveryMethod deliveryMethod;
        @JsonProperty("assigned_location")
        private AssignedLocation assignedLocation;
        @JsonProperty("merchant_requests")
        private List<String> merchantRequests;
    }

    @NoArgsConstructor
    @Data
    public static class Destination {
        private long id;
        private String address1;
        private String address2;
        private String city;
        private String company;
        private String country;
        private String email;
        @JsonProperty("first_name")
        private String firstName;
        @JsonProperty("last_name")
        private String lastName;
        private String phone;
        private String province;
        private String zip;
    }

    @NoArgsConstructor
    @Data
    public static class LineItem {
        private long id;
        @JsonProperty("shop_id")
        private long shopId;
        @JsonProperty("fulfillment_order_id")
        private long fulfillmentOrderId;
        private int quantity;
        @JsonProperty("line_item_id")
        private long lineItemId;
        @JsonProperty("inventory_item_id")
        private long inventoryItemId;
        @JsonProperty("fulfillable_quantity")
        private int fulfillableQuantity;
        @JsonProperty("variant_id")
        private long variantId;
    }

    @NoArgsConstructor
    @Data
    public static class DeliveryMethod {
        private long id;
        @JsonProperty("method_type")
        private String methodType;
        @JsonProperty("min_delivery_date_time")
        private String minDeliveryDateTime;
        @JsonProperty("max_delivery_date_time")
        private String maxDeliveryDateTime;
    }

    @NoArgsConstructor
    @Data
    public static class AssignedLocation {
        private String address1;
        private String address2;
        private String city;
        @JsonProperty("country_code")
        private String countryCode;
        @JsonProperty("location_id")
        private long locationId;
        private String name;
        private String phone;
        private String province;
        private String zip;
    }

    public static void main( String[] args ) {
        String json = "{\n" +
                "    \"fulfillment_orders\": [{\n" +
                "            \"id\": 6348040896566,\n" +
                "            \"shop_id\": 58385104950,\n" +
                "            \"order_id\": 5272249434166,\n" +
                "            \"assigned_location_id\": 64799211574,\n" +
                "            \"request_status\": \"unsubmitted\",\n" +
                "            \"status\": \"open\",\n" +
                "            \"supported_actions\": [\n" +
                "                \"create_fulfillment\",\n" +
                "                \"move\",\n" +
                "                \"hold\",\n" +
                "                \"split\"\n" +
                "            ],\n" +
                "            \"destination\": {\n" +
                "                \"id\": 6161441882166,\n" +
                "                \"address1\": \"2254\",\n" +
                "                \"address2\": null,\n" +
                "                \"city\": \"2\",\n" +
                "                \"company\": null,\n" +
                "                \"country\": \"China\",\n" +
                "                \"email\": \"102@qq.com\",\n" +
                "                \"first_name\": \"fn\",\n" +
                "                \"last_name\": \"ln\",\n" +
                "                \"phone\": null,\n" +
                "                \"province\": \"Inner Mongolia\",\n" +
                "                \"zip\": \"388888\"\n" +
                "            },\n" +
                "            \"line_items\": [{\n" +
                "                    \"id\": 13507453255734,\n" +
                "                    \"shop_id\": 58385104950,\n" +
                "                    \"fulfillment_order_id\": 6348040896566,\n" +
                "                    \"quantity\": 8,\n" +
                "                    \"line_item_id\": 13318773178422,\n" +
                "                    \"inventory_item_id\": 43478270476342,\n" +
                "                    \"fulfillable_quantity\": 8,\n" +
                "                    \"variant_id\": 42129927012406\n" +
                "                }\n" +
                "            ],\n" +
                "            \"international_duties\": null,\n" +
                "            \"fulfill_at\": \"2024-07-11T00:00:00-04:00\",\n" +
                "            \"fulfill_by\": null,\n" +
                "            \"fulfillment_holds\": [],\n" +
                "            \"created_at\": \"2024-07-11T00:59:12-04:00\",\n" +
                "            \"updated_at\": \"2024-07-11T00:59:12-04:00\",\n" +
                "            \"delivery_method\": {\n" +
                "                \"id\": 778602872886,\n" +
                "                \"method_type\": \"shipping\",\n" +
                "                \"min_delivery_date_time\": null,\n" +
                "                \"max_delivery_date_time\": null\n" +
                "            },\n" +
                "            \"assigned_location\": {\n" +
                "                \"address1\": null,\n" +
                "                \"address2\": null,\n" +
                "                \"city\": null,\n" +
                "                \"country_code\": \"CN\",\n" +
                "                \"location_id\": 64799211574,\n" +
                "                \"name\": \"Shop location\",\n" +
                "                \"phone\": null,\n" +
                "                \"province\": null,\n" +
                "                \"zip\": null\n" +
                "            },\n" +
                "            \"merchant_requests\": []\n" +
                "        }, {\n" +
                "            \"id\": 6348040896566,\n" +
                "            \"shop_id\": 58385104950,\n" +
                "            \"order_id\": 5272249434166,\n" +
                "            \"assigned_location_id\": 64799211574,\n" +
                "            \"request_status\": \"unsubmitted\",\n" +
                "            \"status\": \"in_progress\",\n" +
                "            \"supported_actions\": [\n" +
                "                \"create_fulfillment\",\n" +
                "                \"move\",\n" +
                "                \"hold\",\n" +
                "                \"split\"\n" +
                "            ],\n" +
                "            \"destination\": {\n" +
                "                \"id\": 6161441882166,\n" +
                "                \"address1\": \"2254\",\n" +
                "                \"address2\": null,\n" +
                "                \"city\": \"2\",\n" +
                "                \"company\": null,\n" +
                "                \"country\": \"China\",\n" +
                "                \"email\": \"102@qq.com\",\n" +
                "                \"first_name\": \"fn\",\n" +
                "                \"last_name\": \"ln\",\n" +
                "                \"phone\": null,\n" +
                "                \"province\": \"Inner Mongolia\",\n" +
                "                \"zip\": \"388888\"\n" +
                "            },\n" +
                "            \"line_items\": [{\n" +
                "                    \"id\": 13507453255734,\n" +
                "                    \"shop_id\": 58385104950,\n" +
                "                    \"fulfillment_order_id\": 6348040896566,\n" +
                "                    \"quantity\": 8,\n" +
                "                    \"line_item_id\": 13318773178422,\n" +
                "                    \"inventory_item_id\": 43478270476342,\n" +
                "                    \"fulfillable_quantity\": 6,\n" +
                "                    \"variant_id\": 42129927012406\n" +
                "                }\n" +
                "            ],\n" +
                "            \"international_duties\": null,\n" +
                "            \"fulfill_at\": \"2024-07-11T00:00:00-04:00\",\n" +
                "            \"fulfill_by\": null,\n" +
                "            \"fulfillment_holds\": [],\n" +
                "            \"created_at\": \"2024-07-11T00:59:12-04:00\",\n" +
                "            \"updated_at\": \"2024-07-11T10:43:01-04:00\",\n" +
                "            \"delivery_method\": {\n" +
                "                \"id\": 778602872886,\n" +
                "                \"method_type\": \"shipping\",\n" +
                "                \"min_delivery_date_time\": null,\n" +
                "                \"max_delivery_date_time\": null\n" +
                "            },\n" +
                "            \"assigned_location\": {\n" +
                "                \"address1\": null,\n" +
                "                \"address2\": null,\n" +
                "                \"city\": null,\n" +
                "                \"country_code\": \"CN\",\n" +
                "                \"location_id\": 64799211574,\n" +
                "                \"name\": \"Shop location\",\n" +
                "                \"phone\": null,\n" +
                "                \"province\": null,\n" +
                "                \"zip\": null\n" +
                "            },\n" +
                "            \"merchant_requests\": []\n" +
                "        }\n" +
                "    ]\n" +
                "}";
        FulfillmentOrderResponse response = JSON.parseObject(json, FulfillmentOrderResponse.class);
        System.out.println(JSON.toJSONString(response));
        System.out.println(JSON.toJSONString(response.getAllValidFulfillment()));
    }
}
